<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px">
      <el-form-item
        v-if="!this.$route.query.name"
        label="所属学科"
        prop="subjectID"
      >
        <el-select v-model="ruleForm.subjectID" placeholder="请选择">
          <el-option
            v-for="item in subjectList"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item class="inputTag" label="目录名称" prop="tagName">
        <el-input
          v-model="ruleForm.tagName"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>

      <el-form-item class="footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button @click="submit" type="primary">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { add, update } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'tagsAddDialog',
  props: {
    isEdit: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      ruleForm: {
        subjectID: null,
        tagName: null
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      },
      //   学科列表
      subjectList: []
    }
  },
  created () {
    //   //   简单学科列表
    this.getSilple()
  },
  computed: {},
  watch: {
    isEdit: {
      handler (val) {
        if (val) {
          this.ruleForm = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },
  methods: {
    //   简单学科列表
    async getSilple () {
      const res = await simple()
      //   console.log(res, 99)
      this.subjectList = res.data
    },
    submit () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 添加
    async doAdd () {
      if (this.$route.query.name) {
        this.ruleForm.subjectID = Number(this.$route.query.id)
      }
      //  新增标签
      await add(this.ruleForm)
      // this.ruleForm = {
      //   subjectID: null,
      //   tagName: null
      // }
      this.$emit('success')
    },
    // 修改
    async doEdit () {
      const res = await update(this.ruleForm)
      console.log(res)
      this.$emit('success')
    },
    // 取消
    quxiao () {
      this.$emit('success')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input--medium .el-input__inner {
  width: 373px;
}
.footer {
  text-align: right;
}
</style>
